package com.daatak.user.business;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daatak.user.dao.UserDaoI;
import com.daatak.user.domain.UserAddress;
import com.daatak.user.domain.UserObject;
import com.daatak.user.domain.UserRoles;

@Service
@Transactional
public class UserBean implements UserBeanI {
	
	@Autowired
	UserDaoI userDao;
	
	public List<UserObject> list()
	{
		return userDao.list();
	}
	
	@Override
	public void create(UserObject userPojo)
	{
		userDao.create(userPojo);
		
	}
	
	public UserObject findById(Integer Id)
	{
		UserObject user = userDao.findById(Id);
		return userDao.findById(Id);
	}
	
	public void delete(UserObject  userPojo)
	{
		userDao.delete(userPojo);
	}

	public void setUserDao(UserDaoI userDao) {
		this.userDao = userDao;
	}

	public UserObject loadUserByUsername(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<UserAddress> getUserAddress(Integer userId)
	{
		//UserObject user = userDao.getUserAddress(userId);
		
		return userDao.getUserAddress(userId);
	}
	@Override
	public void addRoleToUser(UserObject userObject, String roles[])
	{
		Set<UserRoles> userRoles =new HashSet();
		UserRoles role =null;
		for (int i = 0; i < roles.length; i++) {
			role = new UserRoles();
			role.setRoleCode(roles[i]);
			userRoles.add(role);
		}
		userObject.setRoles(userRoles);
		
	}
	
	@Override
	public String[] getRolesFromUser(UserObject userObject)
	{
		Iterator<UserRoles> userRoles = userObject.getRoles().iterator();
		String roles[] = new String[userObject.getRoles().size()];
		
		for (int i = 0; userRoles.hasNext(); i++) {
			UserRoles userRole = (UserRoles) userRoles.next();
			roles[i] = userRole.getRoleCode();
			
		}
		return roles;
	}
	
}
